package com.example.myapplication.TouchDemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

import com.example.myapplication.R;

public class MultiActionActivity extends AppCompatActivity {

    int mActivePointerId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_action);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mActivePointerId = event.getPointerId(0);
        int pointerIndex = event.findPointerIndex(mActivePointerId);

        float x = event.getX(pointerIndex);
        float y = event.getX(pointerIndex);
        Log.d("MODE", "The Action is: "+actionToString(event.getAction()));
        if(event.getPointerCount()>1){
            for(int i = 0;i<event.getPointerCount();i++){
                 pointerIndex = event.findPointerIndex(i);
                 x = event.getX(pointerIndex);
                 y = event.getX(pointerIndex);
                Log.d("TAG", "Multi Event: "+i+"x: "+x+"y: "+y);
            }
        }else{
            Log.d("TAG", "single Event: "+"x: "+x+"y: "+y);
        }

        return true;
    }

    public static String actionToString(int action){
        switch (action){
            case MotionEvent.ACTION_DOWN:return "Down";
            case MotionEvent.ACTION_MOVE:return "MOVE";
            case MotionEvent.ACTION_CANCEL:return "CANCEL";
            case MotionEvent.ACTION_UP:return "UP";
            case MotionEvent.ACTION_POINTER_UP:return "POINTER_UP";
            case MotionEvent.ACTION_OUTSIDE:return "OUTSIDE";
            case MotionEvent.ACTION_POINTER_DOWN:return "POINTER_DOWN";
        }
        return "";
    }
}